package com.mz.ozy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.bean.PledgeRecordBean;
import com.mz.ozy.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PledgeRecordAdapter extends RecyclerView.Adapter<PledgeRecordAdapter.MyViewHolder> {


    private Context mContext;
    List<PledgeRecordBean.PageBean.PageItemsBean> mBeanList;
    private CancelListener mListener;

    public PledgeRecordAdapter(Context context, List<PledgeRecordBean.PageBean.PageItemsBean> mBeanList) {
        this.mContext = context;
        this.mBeanList = mBeanList;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_pledge_record, parent, false));
    }

    private void initGrade(TextView tvGrade, int grade) {
        switch (grade) {
            case 1:
                tvGrade.setText(mContext.getString(R.string.one_star));
                break;
            case 2:
                tvGrade.setText(mContext.getString(R.string.two_star));
                break;
            case 3:
                tvGrade.setText(mContext.getString(R.string.three_star));
                break;
            case 4:
                tvGrade.setText(mContext.getString(R.string.four_star));
                break;
            case 5:
                tvGrade.setText(mContext.getString(R.string.five_star));
                break;
            default:
                tvGrade.setText(mContext.getString(R.string.common));
                break;
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        PledgeRecordBean.PageBean.PageItemsBean bean = mBeanList.get(position);
        holder.tvDepositTime.setText(mContext.getString(R.string.pledge_time, bean.getCreated()));

        if (!TextUtils.isEmpty(bean.getAmt())) {
            final double rate = Double.parseDouble(bean.getAmt());
            holder.tvMoney.setText(Utils.subTwoNumber(rate));
        } else {
            holder.tvMoney.setText("0.00");
        }
        if (bean.getState() == 0) {
            holder.tvEarned.setText(mContext.getString(R.string.redemption));
        } else {
            holder.tvEarned.setText(mContext.getString(R.string.shu_hui_zhong));
        }
        holder.tvEarned.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    if (bean.getState() == 0) {
                        mListener.cancel(bean.getId(), 0);
                    } else {
                        mListener.cancel(bean.getId(), 1);
                    }
                }
            }
        });
    }

    public void setListener(CancelListener listener) {
        mListener = listener;
    }

    public interface CancelListener {
        void cancel(int id, int type);
    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvMoney)
        TextView tvMoney;
        @BindView(R.id.tv_deposit_time)
        TextView tvDepositTime;
        @BindView(R.id.tvEarned)
        TextView tvEarned;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
